import { PrismaClient } from '@prisma/client'
import { logger } from '../services/logger'

const prisma = new PrismaClient()

const defaultTemplate = {
    version: '1.0.0',
    workflow: {
        nodes: [
            {
                id: 'input',
                type: 'input',
                position: { x: 100, y: 100 }
            },
            {
                id: 'process',
                type: 'process',
                position: { x: 300, y: 100 }
            },
            {
                id: 'output',
                type: 'output',
                position: { x: 500, y: 100 }
            }
        ],
        edges: [
            {
                id: 'e1',
                source: 'input',
                target: 'process'
            },
            {
                id: 'e2',
                source: 'process',
                target: 'output'
            }
        ]
    },
    params: [
        {
            id: 'input_image',
            type: 'image',
            label: '输入图片',
            description: '请上传需要处理的图片',
            required: true
        },
        {
            id: 'style',
            type: 'select',
            label: '处理风格',
            description: '选择图片处理风格',
            options: [
                { label: '写实', value: 'realistic' },
                { label: '卡通', value: 'cartoon' },
                { label: '油画', value: 'oil-painting' }
            ],
            default: 'realistic',
            required: true
        },
        {
            id: 'strength',
            type: 'slider',
            label: '效果强度',
            description: '调整处理效果的强度',
            min: 0,
            max: 100,
            step: 1,
            default: 50
        }
    ],
    layout: {
        groups: [
            {
                id: 'input_group',
                title: '输入设置',
                params: ['input_image'],
                visible: true
            },
            {
                id: 'style_group',
                title: '风格设置',
                params: ['style', 'strength'],
                visible: true
            }
        ]
    }
}

async function createDefaultWorkflow() {
    try {
        const workflow = await prisma.workflow.create({
            data: {
                name: 'default-workflow',
                title: '默认工作流',
                description: '这是一个默认的工作流模板',
                type: 'image',
                workflow: JSON.stringify(defaultTemplate.workflow),
                template: JSON.stringify(defaultTemplate),
                cover: '/images/default-cover.jpg',
                samples: ['/images/sample-1.jpg', '/images/sample-2.jpg'],
                tags: ['默认', '图像处理'],
                isPublic: true,
                serverIds: [],
                excludeServerIds: [],
                powerConsumption: 1,
                sort: 0,
                userId: 'system' // 需要替换为实际的系统用户ID
            }
        })

        logger.info('Default workflow created:', workflow.id)
        return workflow
    } catch (error) {
        logger.error('Failed to create default workflow:', error)
        throw error
    }
}

// 执行创建
if (require.main === module) {
    createDefaultWorkflow()
        .then(() => process.exit(0))
        .catch(error => {
            console.error(error)
            process.exit(1)
        })
}

export { createDefaultWorkflow } 